import Vue from 'vue';
import API from '../../../api/api';

const state = {
    userinfo: null
};

const actions = {
    async getSystemUsers({commit}, uval) {
        var url = API.SYSTEM_USER_URL + '?page=' + uval.pageNum + '&size=' + uval.pageSize;
        if (uval.userName) {
            url += '&userName=' + uval.userName;
        }
        if (uval.status) {
            url += '&status=' + uval.status;
        }
        if (uval.sort) {
            url += '&sort=' + uval.sort;
        }
        var res = await Vue.http.get(url);
        return res;
    },
    async addSystemUser({commit}, uval) {
        var res = await Vue.http.post(API.SYSTEM_USER_URL, uval);
        return res;
    },
    async freezeSystemUser({commit}, uval) {
        var url = API.SYSTEM_USER_URL + uval.userId + '/freeze';
        var res = await Vue.http.put(url);
        return res;
    },
    async unfreezeSystemUser({commit}, uval) {
        var url = API.SYSTEM_USER_URL + uval.userId + '/unfreeze';
        var res = await Vue.http.put(url);
        return res;
    },
    async deleteSystemUser({commit}, uval) {
        var url = API.SYSTEM_USER_URL + uval.userId;
        var res = await Vue.http.delete(url);
        return res;
    },
    async changePassword({commit}, uval) {
        var url = API.CHANGE_PASSWORD_URL;
        var data = {
            oldPassword: uval.oldPass,
            password: uval.newPass,
            confirm: uval.rePass
        };
        var res = await Vue.http.put(url, data);
        return res;
    },
    async resetPassword({commit}, uval) {
        var url = API.RESET_PASSWORD_URL;
        var data = {
            userId: uval.userId,
            password: uval.password
        };
        var res = await Vue.http.put(url, data);
        return res;
    },
    async getMemberUsers({commit}, uval) {
        var url = API.MEMBER_USER_URL + '?page=' + uval.pageNum + '&size=' + uval.pageSize;
        if (uval.userName) {
            url += '&userName=' + uval.userName;
        }
        if (uval.status) {
            url += '&status=' + uval.status;
        }
        var res = await Vue.http.get(url);
        return res;
    }
};

const mutations = {};

export default {
    state,
    actions,
    mutations
};
